﻿Imports System.Collections.Generic

Imports DomusDotNet.Sbm.Services

Imports System.Linq

Imports DomusDotNet.Sbm.ObjectModel

Imports DomusDotNet.Sbm.Services.Interfaces

Imports System

Imports Microsoft.VisualStudio.TestTools.UnitTesting

Imports DomusDotNet.Sbm.Services.Implementations


Namespace DomusDotNet.Sbm.Wcf.Test
    '''<summary>
    '''This is a test class for FakeBookServiceTest and is intended
    '''to contain all FakeBookServiceTest Unit Tests
    '''</summary>
    <TestClass()> _
    Public Class FakeBookServiceTest


        Private testContextInstance As TestContext

        '''<summary>
        '''Gets or sets the test context which provides
        '''information about and functionality for the current test run.
        '''</summary>
        Public Property TestContext() As TestContext
            Get
                Return testContextInstance
            End Get
            Set(value As TestContext)
                testContextInstance = value
            End Set
        End Property

#Region "Additional test attributes"
        '
        'You can use the following additional attributes as you write your tests:
        '
        'Use ClassInitialize to run code before running the first test in the class
        '<ClassInitialize()>  _
        'Public Shared Sub MyClassInitialize(ByVal testContext As TestContext)
        'End Sub
        '
        'Use ClassCleanup to run code after all tests in a class have run
        '<ClassCleanup()>  _
        'Public Shared Sub MyClassCleanup()
        'End Sub
        '
        'Use TestInitialize to run code before running each test
        '<TestInitialize()>  _
        'Public Sub MyTestInitialize()
        'End Sub
        '
        'Use TestCleanup to run code after each test has run
        '<TestCleanup()>  _
        'Public Sub MyTestCleanup()
        'End Sub
        '
#End Region


        '''<summary>
        '''A test for FakeBookService Constructor
        '''</summary>
        <TestMethod()> _
        Public Sub FakeBookServiceConstructor()
            Dim target As FakeBookService = New FakeBookService()
            Assert.IsNotNull(target)
        End Sub


        '''<summary>
        '''A test for GetAll
        '''</summary>
        <TestMethod()> _
        Public Sub GetAll()
            Dim target As IBookService = New FakeBookService()
            Dim expected As IQueryable(Of Global.DomusDotNet.Sbm.ObjectModel.Book) = Nothing
            Dim actual = target.GetAll()
            Assert.IsInstanceOfType(actual, GetType(IQueryable(Of Global.DomusDotNet.Sbm.ObjectModel.Book)))
            Assert.IsNotNull(actual)
        End Sub

        '''<summary>
        '''A test for GetSingle
        '''</summary>
        <TestMethod()> _
        Public Sub GetSingle_BookNotExists()
            Dim target As IBookService = New FakeBookService()
            Dim id As Integer = 0
            Dim expected As Book = Nothing
            Dim actual = target.GetSingle(id)
            Assert.IsNull(actual)
        End Sub

        <TestMethod()> _
        Public Sub GetSingle_BookExists()
            Dim target As IBookService = New FakeBookService()
            Dim id As Integer = 1
            Dim expected As Book = Nothing
            Dim actual = target.GetSingle(id)
            Assert.IsNotNull(actual)
            Assert.IsInstanceOfType(actual, GetType(Global.DomusDotNet.Sbm.ObjectModel.Book))
        End Sub

        Private Function GetAvailableBook(target As IBookService) As Global.DomusDotNet.Sbm.ObjectModel.Book
            Return (From b In target.GetAll()
                       Where b.Available
                       Select b).ToList().FirstOrDefault()
        End Function

        Private Function GetNotAvailableBook(target As IBookService) As Global.DomusDotNet.Sbm.ObjectModel.Book
            Return (From b In target.GetAll()
                       Where Not b.Available
                       Select b).ToList().FirstOrDefault()
        End Function

        '''<summary>
        '''A test for Reserve
        '''</summary>
        <TestMethod()> _
        Public Sub Reserve_BookExists_BookAvailable_ReserveNameValid()
            Dim target As IBookService = New FakeBookService()
            Dim book = GetAvailableBook(target)
            Dim id As Integer = book.ID
            Dim reservationName As String = "username"
            Dim actual As Boolean
            actual = target.Reserve(id, reservationName)
            Assert.IsTrue(actual)
            Assert.IsTrue(target.Errors.Count = 0)
            If actual Then
                Assert.IsNotNull(book)
                Assert.IsFalse(book.Available)
                book.Available = True
            End If
        End Sub

        <TestMethod()> _
        Public Sub Reserve_BookExists_BookAvailable_ReserveNameNotValid()
            Dim target As IBookService = New FakeBookService()
            Dim book = GetAvailableBook(target)
            Dim id As Integer = book.ID
            Dim reservationName As String = String.Empty
            Dim actual As Boolean
            actual = target.Reserve(id, reservationName)
            Assert.IsFalse(actual)
            Assert.IsTrue(target.Errors.Count > 0)
        End Sub

        <TestMethod()> _
        Public Sub Reserve_BookExists_BookNotAvailable_ReserveNameValid()
            Dim target As IBookService = New FakeBookService()
            Dim book = GetNotAvailableBook(target)
            Dim id As Integer = book.ID
            Dim reservationName As String = "username"
            Dim actual As Boolean
            actual = target.Reserve(id, reservationName)
            Assert.IsFalse(actual)
            Assert.IsTrue(target.Errors.Count > 0)
        End Sub

        <TestMethod()> _
        Public Sub Reserve_BookExists_BookNotAvailable_ReserveNameNotValid()
            Dim target As IBookService = New FakeBookService()
            Dim book = GetNotAvailableBook(target)
            Dim id As Integer = book.ID
            Dim reservationName As String = String.Empty
            Dim actual As Boolean
            actual = target.Reserve(id, reservationName)
            Assert.IsFalse(actual)
            Assert.IsTrue(target.Errors.Count > 0)
        End Sub

        <TestMethod()> _
        Public Sub Reserve_BookNotExists_ReserveNameValid()
            Dim target As IBookService = New FakeBookService()
            Dim id As Integer = 0
            Dim reservationName As String = "username"
            Dim actual As Boolean
            actual = target.Reserve(id, reservationName)
            Assert.IsFalse(actual)
            Assert.IsTrue(target.Errors.Count > 0)
        End Sub

        <TestMethod()> _
        Public Sub Reserve_BookNotExists_ReserveNameNotValid()
            Dim target As IBookService = New FakeBookService()
            Dim id As Integer = 0
            Dim reservationName As String = String.Empty
            Dim actual As Boolean
            actual = target.Reserve(id, reservationName)
            Assert.IsFalse(actual)
            Assert.IsTrue(target.Errors.Count > 0)
        End Sub

    End Class
End Namespace